﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace ATCatcher
{
    public partial class Form3 : Form
    {
       
        public Form3()
        {
            InitializeComponent();
        }

        private void Form3_Load(object sender, EventArgs e)
        {  
            
            try
            {
                serialPort1.Open();
                serialPort1.RtsEnable = true;
                //serialPort1.DtrEnable = true;
            }
            catch { MessageBox.Show("串口无法打开!"); this.Close(); }
        }

        private void Form3_FormClosing(object sender, FormClosingEventArgs e)
        {
            serialPort1.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
                string str = textBox1.Text;
                byte[] tx = new byte[str.Length+1];
                for (int i = 0; i < str.Length; i++)
                    tx[i] = (byte)str[i];
                tx[str.Length] = 0x0d;
                serialPort1.Write(tx, 0, str.Length+1);
                //byte x = (byte)serialPort1.ReadByte();
        }

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            byte rx;            
            while (serialPort1.BytesToRead != 0)
            {
                int irx = serialPort1.ReadByte();
                if (irx == -1)
                    break;
                rx = (byte)irx;                
            }
        }
    }
}
